'use strict';

angular.module('xuexibangApp')
	.controller('KlassCtrl', function($scope, $http, Klass, User) {

		// Klass the Klass $resource to fetch all users
		$scope.klasses = Klass.query();
		$scope._id = "";
		$scope.edit = function(klass) {
			$scope._id = klass._id;
			$scope.code = klass.code;
			$scope.name = klass.name;
		}
		$scope.save = function(form) {
			$scope.submitted = true;
			if (form.$valid) {
				var klass = {
					code: $scope.code,
					name: $scope.name
				}
				if ($scope._id != "") {
					klass._id = $scope._id;
				};
				console.log(klass);
				Klass.save(klass, function(data) {
					console.log(data);
				}, function(err) {
					console.log(err);
				});
			}
		};
		$scope.deleteKlass = function(klass) {
			Klass.remove({
				id: klass._id
			}, function(data) {
				console.log(data);
				// angular.forEach($scope.klasses, function(k, i) {
				//   if (k === klass) {
				//     $scope.klasses.splice(i, 1);
				//   }
				// });
			}, function(err) {
				console.log(err);
			});

		};
	})
